package com.hbusa.cotroller.novel;

import com.hbusa.dto.TbNovels;

import com.hbusa.service.service.impl.NovelsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Sandwich
 * @version 1.0
 * @date 2021/12/27-15:12
 */

@WebServlet("/doNovelAdd")
public class novelAddServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;utf-8");

        NovelsServiceImpl novelsService = new NovelsServiceImpl();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        int novelId = Integer.parseInt(req.getParameter("novelId"));
        String novelName = req.getParameter("novelName");
        String author = req.getParameter("author");
        //Date time = req.getParameter("time");
        //字符串转化为java.sql.Date类型
        Date Time = null;
        try {
            Time = dateFormat.parse(req.getParameter("time"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        java.sql.Date time = new java.sql.Date(Time.getTime());

        String classifyId = req.getParameter("classifyId");
        String state = req.getParameter("state");

        int pictureId = Integer.parseInt(req.getParameter("pictureId"));
        int isRecommend = Integer.parseInt(req.getParameter("isRecommend"));
        String describes = req.getParameter("describes");

        novelsService.save(new TbNovels(novelId,novelName,author,time,classifyId,state,pictureId,isRecommend,describes));

        resp.sendRedirect("/doNovelFindAll");

    }
}
